from .models import JobC
from django.db.models import Q
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from .analysis import *
from job.temp_data import data
import re
from django.shortcuts import render,redirect
from django.contrib.auth import authenticate,login,logout
from django.contrib.auth.decorators import login_required

def acc_login(request):
    error_msg = ''
    if request.method == 'GET':
        return render(request,"login.html")
    if request.method == 'POST':
        username = request.POST.get('username')
        print(username)
        password = request.POST.get('pwd')
        print(password)
        user = authenticate(username=username,password = password)
        if user:
            print(user)
            print(type(user))
            login(request,user)
            return redirect(request.GET.get("next",'/job/index'))
        else:
            print(user)
            error_msg = "用户名或密码错误"
        return render(request,"login.html",{"error_msg":error_msg})

def acc_logout(request):
    logout(request)
    return redirect('/login')

@login_required
# 首页
def index(request):
    limit = 20
    location = re.search('index/(.*)', request.path).group(1)
    if location == '' or location == '所有':
        job = JobC.objects.all()
        location = "所有"
    else:
        job = JobC.objects.filter(location__startswith=location)
    paginatior = Paginator(job, limit)
    page = request.GET.get('page', 1)
    loaded = paginatior.page(page)
    city = ['上海', '深圳', '广州', '异地招聘', '杭州', '北京', '南京', '成都', '武汉', '苏州', '西安', '长沙', '大连', '宁波',
            '重庆', '合肥', '无锡', '东莞', '青岛', '福州']
    context = {
        'Job': loaded,
        'counts': job.count(),
        'location': location,
        'City':city,
        'last_time':job.order_by('-release_time').all().first()
    }
    return render(request, 'index_data.html', context)


# 实现搜索及换页
@login_required
def search(request):
    limit = 15
    # 表单中的数据
    search_word = request.GET.get('search')
    if search_word == None:
        search_word = request.GET.get('search_word')
    job = JobC.objects.filter(Q(positionname__contains=search_word))
    pagenum = int(request.GET.get('page', 1))
    p = Paginator(job, limit)
    try:
        loaded = p.page(pagenum)
    except PageNotAnInteger:
        loaded = p.page(1)
    except EmptyPage:
        loaded = p.page(p.num_pages)
    context = {
        'Job': loaded,
        'counts': job.count(),
        'search_word': search_word,
        'last_time': job.order_by('-release_time').all().first()
    }
    return render(request, 'search.html', context)

@login_required
def chart(request):
    # data = chart_data()
    #各城市的职位数
    bar_data = data[0]
    #区域职位数
    pie_data = data[1]
    #职位词频词云
    key_data = data[2]
    #职能词频可下行
    position_num = data[3]
    #职位前二十
    position_detail = data[7]
    #职位城市占比
    key_city = data[4]
    #平均工资
    salary_avg = data[5]
    #职位发布工资范围粗略分布
    key_salary = data[6]
    #职位经验
    key_exp = data[8]
    context = {
        'bar_data': bar_data,
        'pie_data': pie_data,
        'key_data':key_data,
        'position_num':position_num,
        'position_detail':position_detail,
        'key_city':key_city,
        'salary_avg' : salary_avg,
        'key_salary':key_salary,
        'key_exp':key_exp,
        'last_time': JobC.objects.order_by('-release_time').first().release_time
    }
    return render(request, 'chart.html', context)